package com.easy.service.impl;

import com.easy.bean.User;
import com.easy.dao.IUserDao;
import com.easy.service.IUserService;
import com.easy.util.DigestUtil;
import com.easy.util.JWTUtil;
import com.easy.util.Page;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.security.util.Password;

import java.util.List;

import static org.apache.tomcat.jni.SSL.setPassword;

@Service

public class UserService implements IUserService {
    @Autowired
    IUserDao dao;

    private boolean eqPassword(String password,String password_data){
        return  password.equals(password_data);


    }
    @Override
    public String doLogin(User user) {
        //1.获取数据库中的数据  byname
        List<User> list=dao.getUserByUsername(user.getUsername());
        if(list.size()==1){
            User user_data=list.get(0);
            //2.比较密码
            boolean bool=DigestUtil.decode(user.getPassword(),user_data.getPassword());
            if (bool){
                //3.生成token
                return JWTUtil.createToken(null);

            }
        }
        return null;
    }



    @Override
    public List<User> getUser(User user, Page page) {
        return dao.getUser(user,page);
    }

    @Override
    public int addUser(User user) {
        user.setPassword(DigestUtil.randomDigest(user.getPassword()));
        return dao.addUser(user);
    }

    @Override
    public int delUser(int id) {
        return 0;
    }

    @Override
    public User getUserById(int id) {
        return dao.getUserById(id);
    }

    @Override
    public int editUser(User user) {
        int count=dao.editUser(user);
        return count;
    }

    @Override
    public int getCount(User user) {
        return dao.getCount(user);
    }

    @Override
    public User getImg(User user) {
        return dao.getImg(user);
    }

    @Override
    public int Reg(User user) throws Exception {

        if(dao.getUserByUsername(user.getUsername()).size()==0){
            user.setPassword(DigestUtil.randomDigest(user.getPassword()));
           return dao.handleRegister(user);

        }else {
            throw new Exception("用户名重复");
        }

    }

//    @Override
//    public List<User> getUserUsername(User username) {
//        return dao.getUserUsername(username);
//    }


    @Override
    public List<User> getUserByUsername(String username) {
        return dao.getUserByUsername(username);
    }


    @Override
    public int editPassword(String username, String password,String newpassword) throws Exception {
        String hashedPassword = DigestUtil.randomDigest(password);
        if (dao.select(username,hashedPassword)!=null){

            String hashedNewPassword = DigestUtil.randomDigest(newpassword);
            return  dao.editUser(new User(username,hashedNewPassword));
        }else {
            throw new Exception("用户名或原密码错误");
        }




    }


}
